{*
SPDX-FileCopyrightText: © 2021 Olivier Meunier <olivier@neokraft.net>

SPDX-License-Identifier: AGPL-3.0-only
*}
{{ extends "./base" }}
{{ import "/_libs/forms" }}

{{ block title() }}API Token{{ end }}

{{ block mainContent() }}
<h1 class="title text-h2">{{ yield title() }}</h1>

{{ if .Token.IsDeleted -}}
<form action="{{ urlFor(`.`, `delete`) }}" method="POST">
  {{ yield csrfField() }}
  <input type="hidden" name="_to" value="{{ currentPath }}" />
  {{- yield message() content -}}
      Token will be removed in a few seconds&nbsp;
      <button class="btn-primary" name="cancel" value="1">Cancel</button>
  {{- end -}}
</form>
{{- end }}

<h2 class="title text-h3">Properties</h2>

<form class="mb-4" action="{{ urlFor() }}" method="post">
  {{ yield formErrors(form=.Form) }}
  {{ yield csrfField() }}

  <div class="field field-h">
    <label>Token ID:</label>
    <div class="control">{{ .Token.UID }}</div>
  </div>
  <div class="field field-h">
    <label>Application:</label>
    <div class="control">{{ .Token.Application }}</div>
  </div>

  {{ yield checkboxField(
    field=.Form.Get("is_enabled"),
    label="Enabled",
    class="field-h",
  ) }}

  {{ yield dateField(
    field=.Form.Get("expires"),
    label="Expires at",
    class="field-h",
  ) }}

  <h2 class="title text-h3">Roles</h2>

  <p class="my-4 max-w-xl">
    You can limit the permissions granted to this token by choosing one or more
    group of permissions bellow.<br>
    Leave all the choices blank to grant all your permissions to this token.
  </p>

  {{ yield multiSelectField(
    field=.Form.Get("roles"),
    label="Roles",
    class="field-h",
  ) }}

  <p><button class="btn-primary" type="submit">Save</button></p>
</form>

<h2 class="title text-h3">API authentication</h2>

<div class="w-full mb-6" data-controller="clipboard">
  <label class="font-semibold">Your API token</label>
  <button class="link" type="button" data-action="clipboard#copy">
    {{- yield icon(name="o-copy") -}}
  </button>
  <textarea readonly="1" class="form-textarea w-full h-24"
   data-clipboard-target="content">{{ .JWT }}</textarea>
</div>

<p class="mb-2">You can use this token by passing it as an <code>Authorization: Bearer</code>
HTTP header.</p>
<div class="w-full mb-4" data-controller="clipboard">
  <label class="font-semibold">HTTP Header</label>
  <button class="link" type="button" data-action="clipboard#copy">
    {{- yield icon(name="o-copy") -}}
  </button>
  <textarea readonly="1" class="form-textarea w-full h-28"
   data-clipboard-target="content">Authorization: Bearer {{ .JWT }}</textarea>
</div>


{{ if !.Token.IsDeleted -}}
<h2 class="title text-h3">Delete token</h2>
<form action="{{ urlFor(`.`, `delete`) }}" method="POST">
  {{ yield csrfField() }}
  <p><button class="btn-danger">Delete token</button></p>
</form>
{{- end }}

{{ end }}
